from django.shortcuts import HttpResponse
from .models import Article


def index(request):
    # TODO: 查询条件
    # TODO: 1.exact  filed_exact=xxx 等价于 filed=xxx
    # TODO: 使用精确的=进行查找
    # article = Article.objects.filter(title__exact='hello world')
    # article = Article.objects.filter(title='hello world')
    # article = Article.objects.filter(title__exact=None)
    # article = Article.objects.filter(title=None)
    # print(article.query)
    # print(article)

    # TODO: 2.iexact
    # TODO: 使用like进行查找
    article = Article.objects.filter(title__iexact='hello world')
    print(article.query)
    print(article)
    return HttpResponse('success')


def index1(request):
    # TODO: 通过模型的.get返回的数据类型为模型 class
    # TODO: 不能通过query模式输出SQL语句
    article = Article.objects.get(pk=1)
    print(article)
    print(type(article))  # TODO: <class 'article.models.Article'>
    print(isinstance(article, Article))  # TODO: True
    # print(article.query)
    return HttpResponse('success')


def index2(request):
    # TODO: 查询条件
    # TODO；1.contains
    # TODO: 大小写敏感，判断某个字段是否包含了某个数据
    # result = Article.objects.filter(title__contains='hello')
    # print(result.query)
    # print(result)

    # TODO: 2.icontains
    # TODO: 大小写不敏感的匹配查询
    result = Article.objects.filter(title__icontains='Hello')
    print(result.query)
    print(result)
    return HttpResponse('success')


def index3(request):
    # TODO: 查询条件
    # TODO: in - 提取那些给定的field的值是否在给定的容器中。容器可以为list、tuple或者任何一个可以迭代的对象，包括QuerySet对象
    # TODO: 查询ID为1、2、3的文章
    aritcles = Article.objects.filter(id__in = [1, 2, 3])
    print(aritcles.query)
    for aritcle in aritcles:
        print(aritcle)
    return HttpResponse('success')
